﻿using UnityEngine;

using System.Collections;

public class DrawLines : MonoBehaviour
{

    public Material lineMat;

    public GameObject mainPoint;

    public GameObject[] points;

    public GameObject btnGo;


    //public void set
    Canvas canvas;
    public Canvas MainCanvas
    {
        get
        {
            if (null == canvas)
            {
                canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
            }

            return canvas;
        }
    }

    void DrawConnectingLines()

    {
        Vector3 mainPointPos = mainPoint.transform.position;
        if (mainPoint && points.Length > 0)
        {

            // Loop through each point to connect to the mainPoint

            foreach (GameObject point in points)
            {



                Vector3 pointPos = point.transform.position;

                GL.Begin(GL.LINES);

                lineMat.SetPass(0);

                GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));

                GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);

                GL.Vertex3(pointPos.x, pointPos.y, pointPos.z);

                GL.End();

            }

            RectTransform rec = btnGo.transform as RectTransform;
            Vector2 recScreenPos = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, btnGo.transform.position);
            Vector3 worldPosbtn = Camera.main.ScreenToWorldPoint(recScreenPos);
            /* Vector3 pointPos = btnGo.transform as RectTransform;*/

            GL.Begin(GL.LINES);

            lineMat.SetPass(0);

            GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));

            GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);

            /*GL.Vertex3(worldPosbtn.x, worldPosbtn.y, worldPosbtn.z);*/
            GL.Vertex3(-6f, 1, -10);

            GL.End();




        }

    }

    void OnPostRender()

    {

        if (points.Length == 0)

            return;

        DrawConnectingLines();

    }

}